<template>
  <!-- tab基本信息表单  -->
  <el-scrollbar style="height: 100%;margin-top:10px;">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="90px"
      class="form-input-m"
      inline-message
    >
      <el-form-item label="型号名称" prop="modelName">
        <el-input v-model="formData.modelName" placeholder="请输入型号名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="层架数量" prop="shelversNum">
        <el-input v-model="formData.shelversNum" placeholder="请输入层架数量" clearable></el-input>
      </el-form-item>
      <el-form-item label="容积" prop="volume">
        <el-input v-model="formData.volume" placeholder="请输入容积" clearable></el-input>
      </el-form-item>
      <el-form-item label="内部尺寸" prop="inSize">
        <el-input v-model="formData.inSize" placeholder="请输入内部尺寸" clearable></el-input>
      </el-form-item>
      <el-form-item label="冷藏温度范围" prop="frigTempRage">
        <el-input v-model="formData.frigTempRage" placeholder="请输入冷藏温度范围" clearable></el-input>
      </el-form-item>
      <el-form-item label="冷冻温度范围" prop="freezeTempRage">
        <el-input v-model="formData.freezeTempRage" placeholder="请输入冷冻温度范围" clearable></el-input>
      </el-form-item>
      <el-form-item label="温控介入" prop="tempControl">
        <el-input v-model="formData.tempControl" placeholder="请输入温控介入" clearable></el-input>
      </el-form-item>
<!--      <el-form-item label="支持标本盒" prop="specimenBoxType">-->
<!--        <el-input v-model="formData.specimenBoxType" placeholder="请输入支持标本盒类型"         -->
<!--        type="textarea"-->
<!--          style="width: 260px"-->
<!--        clearable></el-input>-->
<!--      </el-form-item>-->
      <!-- <el-form-item label="所属科室" prop="deptName">
        <el-input v-model="formData.deptName" placeholder="请输入所属科室" clearable></el-input>
      </el-form-item> -->
      <br/>
      <el-form-item label="" prop="">
        <el-button type="primary" @click="saveForm">保存</el-button>
      </el-form-item>
    </el-form>

    <!-- 配置扩展属性弹框 -->
    <EditExtraProperties ref="EditExtraPropertiesRef" />
  </el-scrollbar>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import {
  ref,
  reactive,
  watch,
  getCurrentInstance,
  PropType,
  onMounted
} from 'vue';
import { IsEmpty,  formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import EditExtraProperties from '@/components/EditExtraProperties.vue'; //配置扩展属性弹框组件
import { AppStore } from '@/stores/store';
import {  FormInstance } from 'element-plus';
import {samstoFridgeModelAdd,samstoFridgeModelEdit} from "../apis/samsto_api.ts";
// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
const formRef = ref<FormInstance>; //表单ref标记
// 表单绑定的数据类型限制
interface FormDataVO {
  id: string /*主键*/;
    modelName: String ;/*型号名称*/
    shelversNum: String ;/*层架数量*/
    volume: String ;/*容积*/
    inSize: String ;/*内部尺寸*/
    frigTempRage: String ;/*冷藏温度范围*/
    freezeTempRage: String ;/*冷冻温度范围*/
    tempControl: String ;/*温控介入*/
  }
// 表单绑定的数据
let formData = reactive<FormDataVO>({
  id: '',
    modelName:'', /*型号名称*/
    shelversNum:'', /*层架数量*/
    volume:'', /*容积*/
    inSize:'', /*内部尺寸*/
    frigTempRage:'', /*冷藏温度范围*/
    freezeTempRage:'', /*冷冻温度范围*/
    tempControl:'', /*温控介入*/
  });
let types = reactive([]);
// 表单校验规则
const formRules: any = {
  modelName:[{ required: true, message: '请输入型号名称' }],
  shelversNum:[{ required: true, message: '请输入层架数量' }],
  volume:[{ required: true, message: '请输入容积' }],
};

const EditExtraPropertiesRef = ref(null); //配置扩展属性弹框ref

defineExpose({
  addNew,
  saveForm
});

// 父组件传值
let props = defineProps({
//  infoDicts: { type: Array as PropType<any>, required: true },
  currentRow: { type: Object as PropType<any>, required: true }
}) as any;
watch(
  () => props.currentRow,
  (nval, oval) => {
    Object.assign(formData, nval);
  },
  {
    deep: true,
    immediate: true
  }
);

//watch(
//  () => props.infoDicts,
//  (nval, oval) => {
//    types=nval;
//  },
//  {
//    deep: true,
//    immediate: true
//  }
//);


onMounted(() => {
  //formData.bankNumber=generateCode();
});


// 方法定义区 =============
//生成题库编码
function generateCode() {
  let now = new Date();
  console.log(now,"now");
  let y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    h = now.getHours(),
    mm = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return "TM"+y + m+d+h+mm+s+ms;
}
function addNew(callback: any) {
  clear();
  if (callback) {
    callback({ a: '初始化完成' });
  }
}
// 清空表单
function clear() {
  //formData.bankNumber = generateCode();
  formData.id = '';
  formData.modelName = '';//型号名称
  formData.shelversNum = '';//层架数量
  formData.volume = '';//容积
  formData.inSize = '';//内部尺寸
  formData.frigTempRage = '';//冷藏温度范围
  formData.freezeTempRage = '';//冷冻温度范围
  formData.tempControl = '';//温控介入
}

// 保存信息
function saveForm() {
  submitForm();
}

// 保存
const submitForm = () => {
  let elInfo = proxy.$refs.formRef;
  elInfo.validate(async (valid: any) => {
    if (!valid) {
      return false;
    }
    let res: any;
    if (IsEmpty(formData.id)) {
      res = await samstoFridgeModelAdd(formData);
    } else {
      res = await samstoFridgeModelEdit(formData);
    }
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    clear();
    proxy.$emit('findList');
  });
};
// 打开配置扩展属性弹框
function openEditExtraProperties() {
  EditExtraPropertiesRef.value?.toggleDialog();
}

function changeTypes(event: any) {
  const type: any = types.find((item: any) => item.id == event);
  formData.bankType = type.code;
}

function changeStates(event: any) {
  formData.startus = event;
}
const states = [
  {
    value: 1,
    label: '启用'
  },
  {
    value: 0,
    label: '停用'
  }
];
</script>

<style lang="scss" scoped>
// 中等尺寸时 配置扩展属性按钮
.el-button--default {
  padding: 5px 11px;
}
</style>

